<!DOCTYPE html>
<html>
<style>
.container {
    position: relative;
    background-color: pink;
    outline: 1px solid black;
    display: inline-block;
}
.flexbox {
    display: -webkit-flex;
    background-color: grey;
    width: 100px;
    height: 100px;
}
.flexbox > * {
    -webkit-flex: none;
}
.title {
    margin-top: 1em;
}
.ltr {
    direction: ltr;
}
.rtl {
    direction: rtl;
}
.horizontal-tb {
    -webkit-writing-mode: horizontal-tb;
}
.horizontal-bt {
    -webkit-writing-mode: horizontal-bt;
}
.vertical-rl {
    -webkit-writing-mode: vertical-rl;
}
.vertical-lr {
    -webkit-writing-mode: vertical-lr;
}
.row {
    -webkit-flex-flow: row;
}
.row-reverse {
    -webkit-flex-flow: row-reverse;
}
.column {
    -webkit-flex-flow: column;
}
.column-reverse {
    -webkit-flex-flow: column-reverse;
}
.physical {
    margin: 10px 20px 30px 40px;
}
.physical > div {
    margin: 13px auto 17px auto;
}
.logical {
    -webkit-margin-before: 10px;
    -webkit-margin-after: 30px;
    -webkit-margin-start: 40px;
    -webkit-margin-end: 20px;
}
.logical > div {
    -webkit-margin-before: auto;
    -webkit-margin-after: 17px;
    -webkit-margin-start: auto;
    -webkit-margin-end: 2px;
}
.flexbox > :nth-child(1) {
    background-color: blue;
}
.flexbox > :nth-child(2) {
    background-color: green;
}
.flexbox > :nth-child(3) {
    background-color: red;
}
</style>
<script src="../../resources/check-layout.js"></script>
<body onload="checkLayout('.flexbox')">

<script>
function positionAsString(position)
{
    return 'data-offset-x="' + position[0] + '" data-offset-y="' + position[1] + '"';
}

function asString(expectations)
{
    return 'data-expected-width="' + expectations[0] + '" data-expected-height="' + expectations[1] + '" ' + positionAsString(expectations.slice(2, 4));
}

var physicalContainerSize = [160, 140];
var physicalFlexBoxPosition = [40, 10];

var physicalExpectations = {
    'horizontal-tb': {
        rtl: {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 23],
            row: [20, 20, 80, 23],
            'column-reverse': [20, 20, 80, 73],
            'row-reverse': [20, 20, 80, 23],
        },
        ltr : {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 23],
            row: [20, 20, 80, 23],
            'column-reverse': [20, 20, 80, 73],
            'row-reverse': [20, 20, 80, 23],
        }
    },
    'horizontal-bt': {
        rtl: {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 73],
            row: [20, 20, 80, 73],
            'column-reverse': [20, 20, 80, 23],
            'row-reverse': [20, 20, 80, 73],
        },
        ltr : {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 73],
            row: [20, 20, 80, 73],
            'column-reverse': [20, 20, 80, 23],
            'row-reverse': [20, 20, 80, 73],
        }
    },
    'vertical-lr': {
        rtl: {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 73],
            row: [20, 20, 80, 73],
            'column-reverse': [20, 20, 80, 73],
            'row-reverse': [20, 20, 80, 23],
        },
        ltr : {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 23],
            row: [20, 20, 80, 23],
            'column-reverse': [20, 20, 80, 23],
            'row-reverse': [20, 20, 80, 73],
        }
    },
    'vertical-rl': {
        rtl: {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 73],
            row: [20, 20, 80, 73],
            'column-reverse': [20, 20, 80, 73],
            'row-reverse': [20, 20, 80, 23],
        },
        ltr : {
            container: physicalContainerSize,
            flexbox: physicalFlexBoxPosition,
            column: [20, 20, 80, 23],
            row: [20, 20, 80, 23],
            'column-reverse': [20, 20, 80, 23],
            'row-reverse': [20, 20, 80, 73],
        }
    }
}

var logicalExpectations = {
    'horizontal-tb': {
        rtl: {
            container: [160, 140],
            flexbox: [20, 10],
            column: [20, 20, 22, 73],
            row: [20, 20, 22, 73],
            'column-reverse': [20, 20, 22, 73],
            'row-reverse': [20, 20, 22, 73],
        },
        ltr : {
            container: [160, 140],
            flexbox: [40, 10],
            column: [20, 20, 118, 73],
            row: [20, 20, 118, 73],
            'column-reverse': [20, 20, 118, 73],
            'row-reverse': [20, 20, 118, 73],
        }
    },
    'horizontal-bt': {
        rtl: {
            container: [160, 140],
            flexbox: [20, 30],
            column: [20, 20, 22, 47],
            row: [20, 20, 22, 47],
            'column-reverse': [20, 20, 22, 47],
            'row-reverse': [20, 20, 22, 47],
        },
        ltr : {
            container: [160, 140],
            flexbox: [40, 30],
            column: [20, 20, 118, 47],
            row: [20, 20, 118, 47],
            'column-reverse': [20, 20, 118, 47],
            'row-reverse': [20, 20, 118, 47],
        }
    },
    'vertical-lr': {
        rtl: {
            container: [140, 160],
            flexbox: [10, 20],
            column: [20, 20, 73, 22],
            row: [20, 20, 73, 22],
            'column-reverse': [20, 20, 73, 22],
            'row-reverse': [20, 20, 73, 22],
        },
        ltr : {
            container: [140, 160],
            flexbox: [10, 40],
            column: [20, 20, 73, 118],
            row: [20, 20, 73, 118],
            'column-reverse': [20, 20, 73, 118],
            'row-reverse': [20, 20, 73, 118],
        }
    },
    'vertical-rl': {
        rtl: {
            container: [140, 160],
            flexbox: [30, 20],
            column: [20, 20, 47, 22],
            row: [20, 20, 47, 22],
            'column-reverse': [20, 20, 47, 22],
            'row-reverse': [20, 20, 47, 22],
        },
        ltr : {
            container: [140, 160],
            flexbox: [30, 40],
            column: [20, 20, 47, 118],
            row: [20, 20, 47, 118],
            'column-reverse': [20, 20, 47, 118],
            'row-reverse': [20, 20, 47, 118],
        }
    }
}

var writingModes = ['horizontal-tb', 'horizontal-bt', 'vertical-lr', 'vertical-rl'];
var flexFlows = ['row', 'column', 'row-reverse', 'column-reverse'];
var directions = ['ltr', 'rtl'];
var marginTypes = ['physical', 'logical'];

marginTypes.forEach(function(marginType) {
    writingModes.forEach(function(writingMode) {
        flexFlows.forEach(function(flexFlow) {
            directions.forEach(function(direction) {
                var flexboxClassName = writingMode + ' ' + direction + ' ' + flexFlow + ' ' + marginType;
                var title = document.createElement('div');
                title.className = 'title';
                title.innerHTML = flexboxClassName;
                document.body.appendChild(title);

                var expectations = (marginType == 'physical') ? physicalExpectations : logicalExpectations;
                expectations = expectations[writingMode][direction];
                var flexboxExpectations = positionAsString(expectations.flexbox);

                var flexItemExpectations = asString(expectations[flexFlow]);

                var container = document.createElement('div');
                container.className = 'tree';
                container.setAttribute('data-expected-width', expectations.container[0]);
                container.setAttribute('data-expected-height', expectations.container[1]);

                container.innerHTML = '<div class="flexbox ' + flexboxClassName + '" ' + flexboxExpectations + '>\n' +
                    '<div style="height: 20px; width: 20px;" ' + flexItemExpectations + '></div>\n' +
                '</div>';

                document.body.appendChild(container);
            })
        })
    })
})
</script>
</body>
</html>
